package com.example.kotlin.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.BarUtils;


/**
 * @author chenmengjia
 * @date 2022/4/29 下午2:35
 */
public abstract class BasePopupWindow extends PopupWindow {
    protected Context context;
    protected View contentView;

    public BasePopupWindow(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        contentView = LayoutInflater.from(context).inflate(getLayoutId(), null);
        initView(contentView);

        setWidth(getPopWidth());
        setHeight(getPopHeight());

        setFocusable(getFocusable());
        setOutsideTouchable(getOutsideTouchable());
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(contentView);
        //setAnimationStyle(getAnimation());

        contentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (!getIsBackable()) {
                        return true;
                    } else {
                        dismiss();
                    }
                }
                return false;
            }
        });
    }

    /**
     * 布局id
     *
     * @return
     */
    protected abstract int getLayoutId();

    protected int getPopWidth() {
        return ViewGroup.LayoutParams.MATCH_PARENT;
    }

    protected int getPopHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    /**
     * 初始化view
     *
     * @param view
     */
    protected abstract void initView(View view);

    protected boolean getFocusable() {
        return true;
    }

    /**
     * 针对返回进行处理，如果为true的话，则可以进行返回，并关闭pop，如果为false，则拦截返回事件
     *
     * @return
     */
    protected boolean getIsBackable() {
        return true;
    }

    protected boolean getOutsideTouchable() {
        return true;
    }

//    protected int getAnimation() {
//        return com.qmuiteam.qmui.R.style.QMUI_Animation_PopUpMenu_Center;
//    }

    protected boolean isChangeAlpha() {
        return true;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (isChangeAlpha()) {
           backgroundAlpha(1.0f, (Activity) context);

        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        if (isChangeAlpha()) {
           backgroundAlpha(0.4f, (Activity) context);
        }
    }

    @Override
    public void showAsDropDown(View parent, int x, int y, int gravity) {
        //7.0 pop显示问题
        if (getPopHeight() == ViewGroup.LayoutParams.MATCH_PARENT && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Rect visibleFrame = new Rect();
            parent.getGlobalVisibleRect(visibleFrame);
            int height = parent.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height+ BarUtils.getStatusBarHeight());
            super.showAsDropDown(parent, x, y, gravity);
        } else {
            super.showAsDropDown(parent, x, y, gravity);
        }
        if (isChangeAlpha()) {
           backgroundAlpha(0.4f, (Activity) context);
        }
    }


    public void backgroundAlpha(float bgAlpha, Activity activity) {
        Window window = activity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        // 0.0-1.0
        lp.alpha = bgAlpha;
        if (bgAlpha >= 1) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        window.setAttributes(lp);
    }
}
